package irc.view;

import java.awt.Dimension;
import java.util.Enumeration;
import irc.controller.TreeController;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;



/** This shows the tree to the right in the main frame
 * @author Dybdal
 * @version 1.0
 */
public class TreeView {
	private JTree tree;
	private JToolBar toolbar;
	private JScrollPane pane;
	private DefaultMutableTreeNode root;
	private DefaultTreeModel dtm;
	public TreeController TreeC;

	/** constructor for the treeview
	 * @author Dybdal
	 * @since 1.1
	 */
	public TreeView(TreeController TC) {
		TreeC = TC;
		root = new DefaultMutableTreeNode("irc"); // creates a root node
		tree = new JTree(root); // creates a Jtree whit rootnode
		tree.setRootVisible(false); // dont display rootnode
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // gets the treeselection model
		TC.treeSelectionListener(tree); // adds a selection listener to the tree
		dtm = ((DefaultTreeModel) tree.getModel());  // gets the treemodel
		toolbar = new JToolBar(JToolBar.VERTICAL);  // adds the tree to a toolbar
		toolbar.setFloatable(false);	// set the toolbar not floatable
		pane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.setPreferredSize(new Dimension(100, 200)); // adds the toolbar to a scrollpane with pref. size
		toolbar.add(pane); 
	}

	/** return the toolbar
	 * @author Dybdal
	 * @since 1.1
	 * @return Jtoolbar
	 */
	public JToolBar treeIni() {
		return toolbar;
	}

	/** Adds a node the represents the server/netverk
	 * @author Dybdal
	 * @since 1.1
	 */
	public void addServerNode(String name) {
		DefaultMutableTreeNode server = new DefaultMutableTreeNode(name);
		root.insert(server, root.getChildCount());
		dtm.reload();
	}

	/** Add nodes for the channels and the private chats
	 * @author Dybdal
	 * @since 1.1
	 * @param name, server, type
	 */
	public void addNode(String name, String server, String type) { // type = channel/private
		Enumeration<DefaultMutableTreeNode> en = root.preorderEnumeration(); //gets all the nodes in the tree
		while (en.hasMoreElements()) { //loops trough the nodes
			DefaultMutableTreeNode servernode = en.nextElement(); // finds the servernode
			String serverName = servernode.toString();
			if (serverName.equals(server)) { // if the servernode is the right node
				if (servernode.getChildCount() == 0) { // if the server dont have childs
					DefaultMutableTreeNode serverChild = new DefaultMutableTreeNode(type); //adds a channel child
					DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(name); // adds the channel to channel node
					serverChild.add(leaf);
					servernode.insert(serverChild, servernode.getChildCount());
				} else { // if the server have childes
					DefaultMutableTreeNode chanPriv = servernode.getNextNode(); // checks if the the type is a private or channel
					String ChanOrPriv = chanPriv.toString();
					if (ChanOrPriv.equals(type)) {
						DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(name); // checks if the first node is the right node
						chanPriv.insert(leaf, chanPriv.getChildCount()); // adds the node to the parent node
					} else {
						servernode = chanPriv.getNextSibling(); // gets the next node the will be the right node
						if (servernode != null) { // checks that its not null
							DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(name); // adds the node to the parent
							servernode.insert(leaf, servernode.getChildCount());
						} else { // if its null, create private or channel node and add node the that node
							chanPriv = chanPriv.getPreviousNode();
							DefaultMutableTreeNode serverChild = new DefaultMutableTreeNode(type);
							DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(name);
							serverChild.insert(leaf, serverChild.getChildCount());
							chanPriv.insert(serverChild, chanPriv.getChildCount());
						}
					}
				}
				for(int i = 0; i< tree.getRowCount(); i++){ //expanding tree
					tree.expandRow(i);
				}
				tree.updateUI(); // updates the tree
				return; // returns 
			}
		}
	}

	/** remove a selected node
	 * @author Dybdal
	 * @since 1.1
	 * @param name
	 */
	public void removeNode(String name) {
		Enumeration<DefaultMutableTreeNode> en = root.preorderEnumeration(); // gets all nodes in the tree
		while (en.hasMoreElements()) { // loops trough the nodes
			DefaultMutableTreeNode node = en.nextElement(); // gets the nodes name
			String nodeName = node.toString();
			if (nodeName.equals(name)) { // check if its the right node
				if (node.getParent().getChildCount() == 1) {
					dtm.removeNodeFromParent(node.getPreviousNode()); // if parent only have one node remove both
				} else {
					dtm.removeNodeFromParent(node); // remove node
				}
				tree.updateUI(); // update tree
				return; // return 
			}
		}
	}
	
	public void removeServer(){
		dtm.removeNodeFromParent(root.getNextNode());
	}
}